#pragma once
#include <iostream>
#include <pthread.h>
#include <vector>
#include <unistd.h>
namespace MutexModule
{
    class Mutex
    {
    public:
        //禁止拷贝和赋值
        Mutex(const Mutex&) = delete;
        Mutex& operator=(const Mutex&)=delete;
        Mutex()
        {
            pthread_mutex_init(&_mutex, nullptr);
        }
        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }
        void Lock()
        {
            pthread_mutex_lock(&_mutex);
        }
        void Unlock()
        {
            pthread_mutex_unlock(&_mutex);
        }
        pthread_mutex_t * MutexAddr()
        {
            return &_mutex;
        }
    private:
        pthread_mutex_t _mutex;
    };

    //version 2
    class LockGuard
    {
    public:
        LockGuard(Mutex& mutex):_mutex(mutex)
        {
            mutex.Lock();
        }
        ~LockGuard()
        {
            _mutex.Unlock();
        }
    private:
        Mutex& _mutex;
    };
    //version 1
    // class LockGuard
    // {
    // public:
    //     LockGuard(pthread_mutex_t& mutex):_mutex(mutex)
    //     {
    //         pthread_mutex_lock(&_mutex);
    //     }
    //     ~LockGuard()
    //     {
    //         pthread_mutex_unlock(&_mutex);
    //     }
    // private:
    //     pthread_mutex_t& _mutex;
    // };
}